/*
 * @Description:
 * @Version: 2.0
 * @Autor: wsh
 * @Date: 2023-06-20 10:14:50
 * @LastEditors: upholdjx 1373577124@qq.com
 * @LastEditTime: 2023-07-09 14:42:03
 */
import { getAreaObj } from '@/utils/util'
// 列表
const tableConfig = {
	stripe: true,
	border: true,
	maxHeight: '80vh',
	columns: [
		{
			label:'考试名称',
			prop:'examineName'
		},
		{
			label:'创建人',
			prop:'createUserName'
		},
		{
			label:'题库',
			prop:'bankCode',
		},
		{
			label:'状态',
			prop:'state',
			propName:'stateLabel',
			code:'OST_TASK_STATUS',
			api:'getDictItemList',
			type:"remoteSelect"
		},
		{
			label:'需考试人数',
			prop:'examineNum',
		},
		{
			label:'已考',
			prop:'joinNum'
		},
		{
			label:'未考',
			prop:'noJoinNum'
		},
		{
			label:'考试时间',
			prop:'taskTimeRange',
		},
		{
			label:'创建时间',
			prop:'createTime'
		},
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

// 新增编辑
export const BaseFormConfig = ({
	inline: false,
	showBtn: true,
	labelPosition: 'right',
	labelWidth: '120px',
	column: [
		{
			label: '考试名称',
			prop: 'examineName',
			span: 8,
		},
		{
			label: '考试时间',
			prop: 'taskTimeRange',
			type:'datetimerange',
			format:'YYYY年MM月DD HH:mm:ss',
			valueFormat:'YYYY-MM-DD HH:mm:ss',
			span: 16,
		},
		{
			label: '题库分类',
			prop: 'type',
			propName: 'typeLabel',
			// disable:true,
			type: 'remoteSelect',
			code:'OST_QUESTION_BANK_TYPE',
			api:'getDictItemList',
			control: (e) => {
				return {
					bankId: {
						display: e,
					},
				}
			},
			span: 8,
			// rules: [{ required: true, message: '请填写任务名称' }],
		},
		{
			label: '题库题目',
			prop: 'bankId',
			span: 8,
			control: (e) => {
				return {
					itemNum: {
						display: e,
					},
					totalScore: {
						display: e,
					},
				}
			},
			rules: [{ required: true, message: '请选择题库题目' }],
		},
		
		{
			label:"题目数",
			prop:'itemNum',
			disabled:true,
			span: 8
		},
		{
			label:"满分",
			prop:'itemScore',
			disabled:true,
			span: 8,
		},
		{
			label:"本次通过分数",
			prop:'passScore',
			rules: [{required:true, pattern: /^[0-9]*$/, message: '请输入正确的分数' }],
			span: 8
		},
		{
			label: '定向层级',
			type:'radio',
			prop:'directLevel',
			propsName:'directLevelLabel',
			dicData:[
				{label:'按组织',value:'1'},
				{label:'按人员',value:'2'},
			],
			control:(e)=>{
				return {
					organizationIdList:{
						display: e == 1,
					},
					userIdList:{
						display: e == 2,
					},
				}
			},
			span: 8,
			rules: [{ required: true, message: '请填写任务名称' }],
		},
		// 按组织
		{
			label: '考试对象',
			prop: 'organizationIdList',
			span: 8,
			// rules: [{ required: true, message: '请填写任务名称' }],
		},
		// 按人员
		{
			label: '考试对象',
			prop: 'userIdList',
			span: 8,
			// rules: [{ required: true, message: '请填写任务名称' }],
		},
		{
			label: '考试时长',
			prop: 'examineTime',
			type:'text',
			append:'分钟',
			span: 8,
			rules: [{ pattern: /^[1-9]*[1-9][0-9]*$/, message: '请大于等于1的时长' }],
		},
	],
})
export const detailConfig = {
	inline: false,
	showBtn: false,
	labelPosition: 'right',
	labelWidth: '120px',
	column:[]
}

export const baseConfig = {
	pageName: '考核任务管理',
	// 数据字典
	dictcodes: ['OST_TASK_STATUS','OST_QUESTION_BANK_TYPE'],
	rowKey: 'id',
	addLabel: '发布考核任务',
	// 删除插入
	tipLabel: 'taskName',
	api: {
		index: 'view_bscExamineTask_page',
		delete: 'view_bscExamineTask_del',
		add: 'view_bscExamineTask_add',
		edit: 'view_bscExamineTask_edit',
	},
	auth: {
		index: 'view_bscExamineTask_page',
		delete: 'view_bscExamineTask_del',
		add: 'view_bscExamineTask_add',
		edit: 'view_bscExamineTask_edit',
		// 批量设置
		mulitSet: 'mulitSet',
	},
	// 当前行操作按钮
	menuBtn: [
		{
			icon: 'detail',
			label: '详情',
			type: 'primary',
			operate: 'detail',
			auth: '',
			text: true,
			show: true,
		},
	],
	dialogConfig: {
		width: '85%',
	},
	// 搜索
	searchConfig: {
		inline: true,
		showBtn: true,
		labelPosition: 'left',
		column: [
			{ 
				label:'考试名称',
				prop:'examineName',
				span:8
			},
			{
				label: '考试时间',
				prop: 'taskTimeRange',
				type:'datetimerange',
				format:'YYYY年MM月DD HH:mm:ss',
				valueFormat:'YYYY-MM-DD HH:mm:ss',
				span: 16,
			},
			{ 
				label:'状态',
				prop:'state',
				propName:'stateLabel',
				code:'OST_TASK_STATUS',
				api:'getDictItemList',
				type:"remoteSelect",
				span:8
			},
		],
	},
	// 新增
	// formConfig: {
	// 	...BaseFormConfig,
	// 	showBtn: true,
	// },
	// 列表
	tableConfig,
	// 详情
	detailConfig: detailConfig,
	// 处理发布日期
	beforeOnLoadFormate: async (params) => {
		const areasInfo: any = getAreaObj(params.areas_name, '')
		if(Array.isArray(params.taskTimeRange) && params.taskTimeRange.length>0){
			params.startTime = params.taskTimeRange[0]
			params.endTime = params.taskTimeRange[1]
		}
		return Promise.resolve({
			...params,
			...areasInfo
		})
	},
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			let taskTimeRange = null
			if(item.startTime && item.endTime){
				taskTimeRange = item.startTime+'~'+item.endTime
			}
			if(item.joinNum && item.examineNum){
				item.noJoinNum = item.examineNum - item.joinNum
			}
			if(!item.joinNum && item.examineNum){
				item.joinNum = 0
				item.noJoinNum = item.examineNum
			}
			const areasInfo: any = getAreaObj(item, '')
			// 判断状态，结束时间小于当前时间，那么就结束了
			let newDate = new Date().getTime()
			if(item.endTime){
				let endTimeStr = new Date(item.endTime).getTime()
				if(newDate>endTimeStr){
					item.state = 3
				}
			}
			if(item.startTime){
				let startTimeStr = new Date(item.startTime).getTime()
				let endTimeStr = new Date(item.endTime).getTime()
				if(newDate>startTimeStr && newDate<endTimeStr){
					item.state = 2
				}
			}
			if(item.startTime){
				let startTimeStr = new Date(item.startTime).getTime()
				if( newDate<startTimeStr){
					item.state = 1
				}
			}
			return {
				...item,
				...areasInfo,
				taskTimeRange
			}
		})
		return Promise.resolve(tableData)
	},
	// 新增处理
	saveFormate: async (form) => {
		return new Promise((resolve, reject) => {
			const areasObj: any = getAreaObj(form.areas, '')
			if(form.taskTimeRange.length>0){
				form.startTime = form.taskTimeRange[0]
				form.endTime = form.taskTimeRange[1]
			}
			const params = {
				...form,
				...areasObj
			}
			resolve(params)
		})
	},
}
